$(document).ready(function(){
		var server = "cdt";
		$('#wrapper').css('height',$(window).height()-$('.user').height()-$('.recordBtn').height()-100);
		var myScroll,pullDownEl, pullDownOffset,pullUpEl, pullUpOffset,generatedCount = 0;

		var sortTime = 0;
		var getType = 1;
		var	content = "";
		var dataLength = 0;
		var isNoData = 0;

		$.getUser = function(){
			$.getJSON("/"+server+"/UserGet?callback=?&access_token="+$('.access_token').val(),function(json){		
				$('.money1').text("余额："+json.user.money);
				$('.preMoney').text("押金："+json.user.preMoney);
			})
		}

		$.getRecord = function(){
			if(isNoData == 2){
				isNoData = 1;
				$('#thelist').append(
					'<li style="color:#FFFFFF;width:100%;text-align:center;font-size:1em;">'+
						'没有更多的数据了'+
					'</li>'
				)
				return false;				
			}
			else if (isNoData == 1){
				return false;
			}
			$.getJSON("/"+server+"/checkHistoryGet?callback=?&access_token="+$('.access_token').val()+"&type="+getType+"&sortTime="+sortTime,function(json){		
				if (json.result == 1) {
					if(sortTime == 0){
						$('#thelist').empty();
					}
					if(json.data.length > 0){
						for(var i=0;i<json.data.length;i++){
							if(getType == 1){
									var overstepTime = Math.round((json.data[i].expires-json.data[i].serverDate)/3600000);
									var overstepStr = "剩余免费";
									if(overstepTime < 0){
										overstepStr = "超时时间";
										overstepTime = Math.abs(overstepTime);
									}
									content = '<li class="message">'+
												'<div class="column">'+
													'<span>充电宝ID</span>'+
													'<br/>'+
													'<span>借入时间</span>'+
													'<br/>'+
													'<span>借入地点</span>'+
													'<br/>'+
													'<span>'+overstepStr+'</span>'+
													'<br/>'+
													'<br/>'+
													'<span class="cost">产生费用</span>'+
												'</div>'+
												'<div class="details">'+
													'<span>'+json.data[i].cdbSimple+'</span>'+
													'<br/>'+
													'<span>'+json.data[i].addTime+'</span>'+
													'<br/>'+
													'<span>'+json.data[i].shopModel.name+'</span>'+
													'<br/>'+
													'<span>'+overstepTime+'小时</span>'+
													'<br/>'+
													'<br/>'+
													'<span class="cost">￥'+json.data[i].overtimeMoney+'</span>'+
												'</div>'+
											'</li>'+
											'<hr/>'
							}						
							else if(getType == 2){
								content = '<li class="message">'+
											'<div class="column">'+
												'<span>充电宝ID</span>'+
												'<br/>'+												
												'<span>借入时间</span>'+
												'<br/>'+
												'<span>归还时间</span>'+
												'<br/>'+
												'<span>借入地点</span>'+
												'<br/>'+
												'<br/>'+
												'<span class="cost">产生费用</span>'+
											'</div>'+
											'<div class="details">'+
												'<span>'+json.data[i].cdbSimple+'</span>'+
												'<br/>'+											
												'<span>'+json.data[i].addTime+'</span>'+
												'<br/>'+
												'<span>'+json.data[i].returnTime+'</span>'+
												'<br/>'+
												'<span>'+json.data[i].shopModel.name+'</span>'+
												'<br/>'+	
												'<br/>'+
												'<span class="cost">￥'+json.data[i].overtimeMoney+'</span>'+
											'</div>'+
										'</li>'+
										'<hr/>'

								
							}
							else if(getType == 3){
									var lineName = "";
									if(json.data[i].lineType == 1){
										lineName = "iphone5/6数据线"
									}
									else if(json.data[i].lineType == 2){
										lineName = "iphone4数据线"
									}
									else if(json.data[i].lineType == 3){
										lineName = "android数据线"
									}
									content = '<li class="message">'+
											'<div class="column">'+
												'<span>商品名称</span>'+
												'<br/>'+
												'<span>购买时间</span>'+
												'<br/>'+
												'<span>购买地点</span>'+
												'<br/>'+
												'<br/>'+
												'<span class="cost">消费金额</span>'+
											'</div>'+
											'<div class="details">'+
												'<span>'+lineName+'</span>'+
												'<br/>'+
												'<span>'+json.data[i].addTime+'</span>'+
												'<br/>'+
												'<span>'+json.data[i].shopModel.name+'</span>'+
												'<br/>'+
												'<br/>'+
												'<span class="cost">￥'+json.data[i].cost+'</span>'+
											'</div>'+
										'</li>'+
										'<hr/>'
							}
							if(sortTime == 0){
								$('#thelist').append(content);
							}
							else{
								$('#thelist hr').eq($('#thelist hr').length-1).after(content);	
							}															
						}
						sortTime = json.sortTime;

					}
					else{
							$('#thelist').append(
								'<li style="color:#FFFFFF;width:100%;text-align:center;font-size:1em;">'+
									'没有更多的数据了'+
								'</li>'
							)
					}

					if (json.data.length == 0){
							isNoData = 1;
					}
					else if(json.data.length < 10){
							isNoData = 2;
					}
				}
				else {
						$('#thelist').append(
							'<li style="color:#FFFFFF;width:100%;text-align:center;font-size:1em;">'+
								'网络错误'+
							'</li>'
						)					
				}
				dataLength = json.data.length;
				if(dataLength < 10){
					$('#pullUp').hide();
				}
				myScroll.refresh();
			})
		}

		/**
		 * 下拉刷新 （自定义实现此方法）
		 * myScroll.refresh();		// 数据加载完成后，调用界面更新方法
		 */
		function pullDownAction () {
			// setTimeout(function () {	// <-- Simulate network congestion, remove setTimeout from production!
				isNoData = 0;
				sortTime = 0;
				$.getRecord();
				$.getUser();
				myScroll.refresh();		//数据加载完成后，调用界面更新方法   Remember to refresh when contents are loaded (ie: on ajax completion)
			// }, 500);	// <-- Simulate network congestion, remove setTimeout from production!
		}

		/**
		 * 滚动翻页 （自定义实现此方法）
		 * myScroll.refresh();		// 数据加载完成后，调用界面更新方法
		 */
		function pullUpAction () {
			// setTimeout(function () {	// <-- Simulate network congestion, remove setTimeout from production!
				$.getRecord();
				myScroll.refresh();		// 数据加载完成后，调用界面更新方法 Remember to refresh when contents are loaded (ie: on ajax completion)
			// }, 500);	// <-- Simulate network congestion, remove setTimeout from production!
		}

		/**
		 * 初始化iScroll控件
		 */
		$.loaded = function() {
			pullDownEl = document.getElementById('pullDown');
			pullDownOffset = pullDownEl.offsetHeight;
			pullUpEl = document.getElementById('pullUp');	
			pullUpOffset = pullUpEl.offsetHeight;
			
			myScroll = new iScroll('wrapper', {
				scrollbarClass: 'myScrollbar', /* 重要样式 */
				useTransition: false, /* 此属性不知用意，本人从true改为false */
				topOffset: pullDownOffset,
				onRefresh: function () {
					if (pullDownEl.className.match('loading')) {
						pullDownEl.className = '';
						pullDownEl.querySelector('.pullDownLabel').innerHTML = '下拉刷新...';
					} else if (pullUpEl.className.match('loading')) {
							pullUpEl.className = '';
							pullUpEl.querySelector('.pullUpLabel').innerHTML = '上拉加载更多...';
					}
				},
				onScrollMove: function () {
					if (this.y > 5 && !pullDownEl.className.match('flip')) {
						pullDownEl.className = 'flip';
						pullDownEl.querySelector('.pullDownLabel').innerHTML = '松手开始更新...';
						this.minScrollY = 0;
					} else if (this.y < 5 && pullDownEl.className.match('flip')) {
						pullDownEl.className = '';
						pullDownEl.querySelector('.pullDownLabel').innerHTML = '下拉刷新...';
						this.minScrollY = -pullDownOffset;
					} else if (this.y < (this.maxScrollY - 5) && !pullUpEl.className.match('flip')) {
						pullUpEl.className = 'flip';
						pullUpEl.querySelector('.pullUpLabel').innerHTML = '松手开始更新...';
						this.maxScrollY = this.maxScrollY;
					} else if (this.y > (this.maxScrollY + 5) && pullUpEl.className.match('flip')) {
						pullUpEl.className = '';
						pullUpEl.querySelector('.pullUpLabel').innerHTML = '上拉加载更多...';
						this.maxScrollY = pullUpOffset;
					}
				},
				onScrollEnd: function () {
					if (pullDownEl.className.match('flip')) {
						pullDownEl.className = 'loading';
						pullDownEl.querySelector('.pullDownLabel').innerHTML = '加载中...';				
						pullDownAction();	// Execute custom function (ajax call?)
					} else if (pullUpEl.className.match('flip')) {
						pullUpEl.className = 'loading';
						pullUpEl.querySelector('.pullUpLabel').innerHTML = '加载中...';	
						pullUpAction();	// Execute custom function (ajax call?)
					}
				}
			});
			
			// setTimeout(function () { document.getElementById('wrapper').style.left = '0'; }, 800);
		}

		//初始化绑定iScroll控件 
		document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);
		$.getRecord();
		$.loaded();
		// document.addEventListener('DOMContentLoaded', loaded, false); 


	$('.borrowRecord').bind('click',function(){
		$('.btn').css('background','#E5E5E5').css('border-color','#393837').css('color','#393837');
		$(this).css('background','#393837').css('border-color','#393837').css('color','#A7A7A7');
		getType = 1;
		sortTime = 0;
		isNoData = 0;
		pullDownAction();
	})

	$('.returnRecord').bind('click',function(){
		$('.btn').css('background','#E5E5E5').css('border-color','#393837').css('color','#393837');
		$(this).css('background','#393837').css('border-color','#393837').css('color','#A7A7A7');
		getType = 2;
		sortTime = 0;
		isNoData = 0;
		pullDownAction();
	})
	$('.buyRecord').bind('click',function(){
		$('.btn').css('background','#E5E5E5').css('border-color','#393837').css('color','#393837');
		$(this).css('background','#393837').css('border-color','#393837').css('color','#A7A7A7');
		getType = 3;
		sortTime = 0;
		isNoData = 0;
		pullDownAction();
	})			

})